// pages/moral/moral.js
const host=getApp().host;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    admin:{},
    pageSize:10,
    list:[],
    sum:0,
    total:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      admin:wx.getStorageSync('admin')
    },()=>{
      this.initMoral()
      this.initSum();
    })
  },

  /**
   * 初始化列表 
   * */
  initMoral(){
    wx.showLoading({
      title: '加载列表中',
    })
    wx.request({
      url: host+'/client/moral/list?page=0&user='+this.data.admin.loginName+'&pageSize='+this.data.pageSize,
      success:res=>{
        let list=res.data.data.list;
        list.forEach(item=>{
          item.date=this.formatDate(item.date)
        })
        this.setData({
          list:list,
          total:res.data.data.total
        })
      },
      complete:()=>{
        wx.hideLoading()
      }
    })
  },

  /**
   * 初始化德育分总分
   */
  initSum(){
    wx.request({
      url: host+'/client/moral/sum?user='+this.data.admin.loginName,
      success:res=>{
        this.setData({
          sum:res.data.data.sum
        })
      }
    })
  },

  /**格式化日期 */
  formatDate(row) {
    let date = new Date(row);
    let month =
          date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1;
    let day =
          date.getDate() < 10
                ? "0" + date.getDate()
                : date.getDate();
    let hours =
          date.getHours() < 10
                ? "0" + date.getHours()
                : date.getHours();
    let minutes =
          date.getMinutes() < 10
                ? "0" + date.getMinutes()
                : date.getMinutes();
    return (
          date.getFullYear() +
          "-" +
          month +
          "-" +
          day +
          " " +
          hours +
          ":" +
          minutes
    );
  },

  /**下拉触底 */
  onReachBottom(){
    if(this.data.pageSize<this.data.total){
      this.setData({
        pageSize:this.data.pageSize+10
      },()=>{
        this.initMoral()
      });
    }else{
      wx.showToast({
        title: '没有更多了',
        icon:'error'
      })
    }
  }
})